# -*- coding: utf-8 -*-

import os.path
import selector
from amplee.loader import loader

base_dir = os.getcwd()

__all__ = ['base_dir']

import sys
sys.path.append(base_dir)
from atompub import AtomPub


def get_service_document(environ, start_response):
    start_response("200 OK", [('Content-type', 'application/atomsvc+xml')])  
    return [service.to_service().xml()]

def setup_apps():
    globals()['service'], globals()['conf'] = loader(os.path.join(base_dir, 'blog.conf'),encoding='ISO-8859-1', base_path=base_dir)
    s = selector.Selector()
    s.add('/pub', GET=get_service_document)
    
    workspace = service.get_workspace('blog')
    
    store = AtomPub(workspace.get_collection('music'))
    s.add('/pub/music[/]', GET=store.GET, POST=store.POST, HEAD=store.HEAD)
    s.add('/pub/music/{id}[.{format}]', GET=store.GET, PUT=store.PUT, DELETE=store.DELETE, HEAD=store.HEAD)
   
    from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
    httpd = WSGIServer(('', 8080), WSGIRequestHandler)
    httpd.set_app(s)
    print "Serving HTTP on %s port %s ..." % httpd.socket.getsockname()
    httpd.serve_forever()

   
if __name__ == '__main__':
    setup_apps()
